'use strict';
const db = uniCloud.database();

/**
 * 云函数：获取题目筛选选项
 * @returns {Object} - 分类和难度列表
 */
exports.main = async (event, context) => {
  try {
    const questionCollection = db.collection('questions');
    
    // 获取唯一分类
    const categoriesRes = await questionCollection
      .aggregate()
      .group({ _id: '$category' })
      .project({ _id: 0, category: '$_id' })
      .end();
    
    // 获取唯一难度
    const difficultiesRes = await questionCollection
      .aggregate()
      .group({ _id: '$difficulty' })
      .project({ _id: 0, difficulty: '$_id' })
      .end();
    
    return {
      code: 0,
      data: {
        categories: categoriesRes.data.map(item => item.category),
        difficulties: difficultiesRes.data.map(item => item.difficulty)
      },
      msg: '获取筛选选项成功'
    };
  } catch (e) {
    console.error('获取筛选选项失败:', e);
    return { code: -1, msg: `获取筛选选项失败: ${e.message || '未知错误'}` };
  }
};